package com.ojoin.ojoin.eb_spider.service.catchdata.suning;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.ISLDataCatchAtomStepService;
import com.ojoin.ojoin.eb_spider.service.SupplyPaymentService;
import com.ojoin.ojoin.eb_spider.service.WebDriverService;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.NumberUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;

@Scope("prototype")
@Service
public class SUNINGPayAccount implements ISLDataCatchAtomStepService<TbSlSupplyPayment> {

	@Autowired
	private SupplyPaymentService paymentService;
	
	
	public WebDriverService webDriver;
	public TbSlPlatformUser platformUser;
	public String stime ;
	public String etime ;
	
	
	private Logger log = LoggerFactory
			.getLogger(SUNINGPayAccount.class);
	
	@Override
	public void get() {
		webDriver
				.get("http://scs.suning.com/sps/paymentbill/getPaymentBillInfo.action?mark=1");

	}

	@Override
	public void sendKeys() {
		webDriver.sendKeys("id", "startPayDate", stime);
		webDriver.sendKeys("id", "endPayDate", etime);
		
	}

	@Override
	public void submit() {
		webDriver.click("id", "searchBtn");
	}

	@Override
	public void run() {
		get();
		sendKeys();
		submit();
	}

	@Override
	public List<TbSlSupplyPayment> result() {
		List<TbSlSupplyPayment> payments = new ArrayList<TbSlSupplyPayment>();

		boolean flag = true;
		// 是否有下一页
		while (flag) {

			// findElement(attribute);
			WebElement table = webDriver.findElement("id", "gridTable");

			List<WebElement> trs = table.findElements(By.tagName("tr"));
			Iterator<WebElement> it = trs.iterator();
			while (it.hasNext()) {
				WebElement tr = it.next();
				if(webDriver.findElements(tr,"tagName", "td").size() > 2){
					TbSlSupplyPayment payment = this
							.getTbSlSupplyPaymentByWebElement(tr);
					payments.add(payment);
				}else {
					return payments;
				}
				
			}

			flag = hasNextPage();
		}
		
		
		return payments;

	}

	public void fiter(List<TbSlSupplyPayment> list,Map<String, TbSlSupplyPayment> map) {

		

		paymentService.removePaymentCommon(list, map);

		
		initTbSlSupplyPaymentByWebElement(list);


	}

	public TbSlSupplyPayment getTbSlSupplyPaymentByWebElement(WebElement tr) {
		
		String trid = webDriver.getTextByJS(tr, "id", "paymentdetail");

		String time = webDriver.getTextByJS(tr, "css", "td:nth-child(7)  div");

		String samount = webDriver.getTextByJS(tr, "css",
				"td:nth-child(5)  div");
		
		long amount = NumberUtils.getLongAmount(samount);

		TbSlSupplyPayment payment = new TbSlSupplyPayment();

		payment.setPlatformAccountId(platformUser.getId());

		payment.setPayId(trid);
		payment.setCreateDate(time);
		payment.setPayDate(TimeUtils.getCurDate());

		payment.setAmount(amount);
		payment.setSettlementState(TbSlSupplyPayment.COMPLETEPAY);

		return payment;
	}

	/**
	 * 更新付款单与结算单对应信息
	 * @param payments
	 * @return
	 */
	public List<TbSlSupplyPayment> initTbSlSupplyPaymentByWebElement(
			List<TbSlSupplyPayment> payments) {
		List<TbSlSupplyPayment> real = new ArrayList<TbSlSupplyPayment>();

		Iterator<TbSlSupplyPayment> it = payments.iterator();
		while (it.hasNext()) {
			TbSlSupplyPayment tbsl = it.next();

			String Purchase = "http://scs.suning.com/sps/paymentbill/getPaymentBillDetail.action?paymentCode="
					+ tbsl.getPayId();

			webDriver.get(Purchase);

			WebElement webson = webDriver.findElement("id", "gridTable");

			List<WebElement> trsson = webson.findElements(By.tagName("tr"));

			if (null != trsson && !trsson.isEmpty()) {
				for (int i = 0; i < trsson.size(); i++) {

					WebElement tr = trsson.get(i);

					WebElement num = tr.findElement(By
							.cssSelector("td:nth-child(2) > div"));

					long amount = NumberUtils.getLongAmount(num.getText());

					String inpayid = webDriver.getTextByJS(tr, "css",
							"td:nth-child(1) > div");

					if (CommonUtils.isEmpty(inpayid)) {
						inpayid = tbsl.getPayId();
					}

					if (i > 0) {
						LogUtils.info(log, "tbsl.getPayId()"+tbsl.getPayId()+"结算单超过一单");
						TbSlSupplyPayment payment = new TbSlSupplyPayment();
						payment.setPlatformAccountId(platformUser.getId());

						payment.setPayId(tbsl.getPayId());
						payment.setCreateDate(tbsl.getCreateDate());

						payment.setAmount(amount);
						payment.setSettlementState(TbSlSupplyPayment.COMPLETEPAY);
						tbsl.setSettlementId(inpayid);
					} else {
						tbsl.setSettlementId(inpayid);
						tbsl.setAmount(amount);
					}
					real.add(tbsl);
				}
			}

		}

		return real;
	}

	public boolean hasNextPage() {

		WebElement ele = webDriver.findElement("css", "#pageFrm div .next");

		if (ele.getTagName().equals("span")) {
			return false;
		} else {
			webDriver.clickByJS(ele);
			return true;
		}

	}

	public void init(WebDriverService webDriver, TbSlPlatformUser platformUser,String stime,String etime) {
		this.webDriver = webDriver;

		this.platformUser = platformUser;
		this.stime = stime;
		this.etime = etime;
		

	}


}
